package com.hxkcloud.nurse.popup;

import android.app.Activity;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.PopupWindow;

import com.hxkcloud.nurse.R;
import com.hxkcloud.nurse.utils.BrightnessUtils;
import com.hxkcloud.nurse.wheelview.WheelView;
import com.hxkcloud.nurse.wheelview.adapter.ArrayWheelAdapter;

import java.util.ArrayList;

/**
 * Created by william_sun on 2016-07-30.
 * Description:选择提醒单的1-50天
 */
public class DayIndexSelectPopWindow extends PopupWindow implements View.OnClickListener {
    private Activity context;
    private WheelView wheel_day;

    private Button bt_cancel;
    private Button bt_sure;

    private ArrayList<String> days;

    public DayIndexSelectPopWindow(Activity context) {
        this.context = context;
        View rootView = LayoutInflater.from(context).inflate(R.layout.popup_selectaddress, null);
        setContentView(rootView);

        days = new ArrayList<>();
        for (int i = 0; i < 50; i++) {
            days.add("第" + (i + 1) + "天");
        }

        setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);

        setFocusable(true);
        setTouchable(true);
        setOutsideTouchable(true);
        setBackgroundDrawable(new ColorDrawable(0x00000000));

        setAnimationStyle(R.style.popupwindow_anim_default);
        initViews(rootView);
        initDatas();
        initListener();
    }

    private void initViews(View rootView) {
        wheel_day = (WheelView) rootView.findViewById(R.id.wheel_selectaddress_add);
        bt_cancel = (Button) rootView.findViewById(R.id.bt_selectaddress_cancel);
        bt_sure = (Button) rootView.findViewById(R.id.bt_selectaddress_sure);
    }

    private void initDatas() {
        String[] fs = new String[days.size()];
        for (int i = 0; i < days.size(); i++) {
            fs[i] = days.get(i);
        }
        ArrayWheelAdapter adapter = new ArrayWheelAdapter(context, fs);
        wheel_day.setViewAdapter(adapter);
        wheel_day.setCyclic(false);

        //设置显示行数
        wheel_day.setVisibleItems(5);

    }

    private void initListener() {
        bt_sure.setOnClickListener(this);
        bt_cancel.setOnClickListener(this);
        setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss() {
                BrightnessUtils.lightOn(context);
            }
        });
    }

    @Override
    public void onClick(View v) {
        dismiss();
        switch (v.getId()) {
            case R.id.bt_selectaddress_cancel:

                break;
            case R.id.bt_selectaddress_sure:
                if (dayListener != null) {
                    dayListener.selectDay(days.get(wheel_day.getCurrentItem()));
                }
                break;
        }
    }

    public void show(View parent) {
        showAtLocation(parent, Gravity.BOTTOM, 0, 0);
        BrightnessUtils.lightOff(context);
    }

    public interface onDaySelectListener {
        void selectDay(String day);
    }

    private onDaySelectListener dayListener;

    public void setDayListener(onDaySelectListener dListener) {
        this.dayListener = dListener;
    }
}
